{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a4dfc83e",
   "metadata": {},
   "source": [
    "## Q1\n",
    "* 查找\"媒体\"的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9830e6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \n",
    "freq_table_phrase=\n",
    "freq_table_phrase\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4819f038",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 答案\n",
    "phrase = \"媒体\"\n",
    "freq_table_phrase=text.count(phrase)\n",
    "freq_table_phrase\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0559b0a",
   "metadata": {},
   "source": [
    "#### 补充说明\n",
    "Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。\n",
    "\n",
    "count()方法语法：\n",
    "text.count(sub, start= 0,\n",
    "end=len(string))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f84f4e9d",
   "metadata": {},
   "source": [
    "## Q2\n",
    "* 用中文\"。\"拆分,生成list_split列表，每一个句子是一个独立的列表元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c2312c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "list_split =\n",
    "list_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a8b76e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 答案\n",
    "list_split =text.split(\"。\")\n",
    "list_split"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdbb7c13",
   "metadata": {},
   "source": [
    "#### 补充说明\n",
    "Python split() 通过指定分隔符对字符串进行切片，如果参数 num 有指定值，则分隔 num+1 个子字符串\n",
    "\n",
    "split() 方法语法： text.split(str=\"\", num=string.count(str))."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "523aa2ed",
   "metadata": {},
   "source": [
    "\n",
    "## Q3 \n",
    "* 取出第十个句子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfd029f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "the_10_phrase=\n",
    "the_10_phrase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "783e721c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 答案\n",
    "the_10_phrase=list_split[9]\n",
    "the_10_phrase"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54fb4a8d",
   "metadata": {},
   "source": [
    "#### 补充说明\n",
    "本题知识点与Q2类似，切片后直接取出，注意，句子是从零开始，故而第十句取9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d17b09a1",
   "metadata": {},
   "source": [
    "## Q4  \n",
    "* 请找出text中所有\"新媒体\"关键字前面的两个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed92ed06",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase=\n",
    "\n",
    "position_all=[] # \"媒体\"  出现的位置\n",
    "\n",
    "content_all=[]\n",
    "\n",
    "    \n",
    "print(content_all)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a4995fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 答案\n",
    "phrase=\"媒体\"\n",
    "\n",
    "position_all=[] # \"媒体\"  出现的位置\n",
    "for i,c in enumerate(text):\n",
    "    if c==phrase[0]:\n",
    "        if i<len(text):\n",
    "            if text[i+1]==phrase[1]:\n",
    "                position_all.append(i)\n",
    "print(position_all) \n",
    "content_all=[]\n",
    "for i in position_all:\n",
    "    content_all.append(text[i-2:i])\n",
    "    \n",
    "print(content_all)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d90ccb2",
   "metadata": {},
   "source": [
    "#### enumerate()   枚举\n",
    "因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历索引\n",
    "因此，有了枚举的方法，同时遍历两者"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b3e7319",
   "metadata": {},
   "source": [
    "## Q5 \n",
    "* 统计text中所有\"新媒体\"关键字前面的两个字符的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54f1fee5",
   "metadata": {},
   "outputs": [],
   "source": [
    "found = {}\n",
    "for i in content_all:\n",
    "      found [i] += 1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2704be0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 答案\n",
    "found = {}\n",
    "found = found.fromkeys(content_all,0)\n",
    "for i in content_all:\n",
    "    if i in content_all:\n",
    "        found[i]+=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81c691",
   "metadata": {},
   "source": [
    "## Q6 \n",
    "找出text中所有\"新媒体\"关键字前面的两个字符的次数排在前五的关键词，作为一个新的字典输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9232bfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "top8_found = {}\n",
    "\n",
    "top8_found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad6569a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 答案\n",
    "top8_found = {}\n",
    "for i in  sorted(found.values(), reverse=True)[:8]:\n",
    "    for k,v in found.items():\n",
    "        if v == i:\n",
    "            top8_found[k] = v\n",
    "top8_found"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
